package example;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TestImg extends JFrame implements ActionListener {
	private static final long serialVersionUID = 1L;
	Image img[] = new Image[5];
	int index = 0;

	public TestImg() {
		for (int i = 0; i < 5; i++) {
			img[i] = Toolkit.getDefaultToolkit().createImage(
					"image" + i + ".jpg");
		}
		setLayout(new BorderLayout());
		ImagePanel panel = new ImagePanel();
		add(panel, BorderLayout.CENTER);
		JButton jbtNext = new JButton("Next");
		jbtNext.addActionListener(this);
		add(jbtNext, BorderLayout.SOUTH);
	}

	public static void main(String[] args) {
		TestImg frame = new TestImg();
		frame.setTitle("TestImg");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(550, 400);
		frame.setVisible(true);
	}

	// inner class
	class ImagePanel extends JPanel {
		private static final long serialVersionUID = 1L;

		protected void paintComponent(Graphics g) {
			super.paintComponent(g);
			g.drawImage(img[index], 0, 0, this);
			g.drawImage(img[index], 260, 0, 510, 150, 250, 0, 0, 150, this);
			g.drawImage(img[index], 0, 160, 250, 310, 0, 150, 250, 0, this);
			g.drawImage(img[index], 260, 160, 510, 310, 250, 150, 0, 0, this);
		}
	}

	public void actionPerformed(ActionEvent e) {
		if (index == 4)
			index = 0;
		else
			index++;
		this.repaint();
	}
}